package com.hello.sky.controller.user;

import com.hello.sky.model.dto.OrdersPageQueryDTO;
import com.hello.sky.model.dto.OrdersPaymentDTO;
import com.hello.sky.model.dto.OrdersSubmitDTO;
import com.hello.sky.model.vo.OrderPaymentVO;
import com.hello.sky.model.vo.OrderSubmitVO;
import com.hello.sky.model.vo.OrderVO;
import com.hello.sky.result.PageResult;
import com.hello.sky.result.Result;
import com.hello.sky.service.OrdersService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author D
 */
@Slf4j
@RestController("userOrderCotroller")
@RequestMapping("/user/order")
public class OrdersController {

    @Autowired
    private OrdersService  ordersService;

    @PostMapping("/submit")
    public Result submitOrder(@RequestBody OrdersSubmitDTO orderDot){
        OrderSubmitVO orderSubmitVO = ordersService.submitOrder(orderDot);
        return Result.success(orderSubmitVO);
    }

    @PutMapping("/payment")
    @Operation(summary = "订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = ordersService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    @GetMapping("/historyOrders")
    @Operation(summary = "历史订单查询")
    public Result<PageResult> historyOrdersPage(OrdersPageQueryDTO dto){
       PageResult pageResult = ordersService.historyOrdersPage(dto);
       return Result.success(pageResult);
    }

    @GetMapping("/orderDetail/{id}")
    @Operation(summary = "查询订单详情")
    public Result<OrderVO> orderHistoryByOrderId(@PathVariable("id")Long orderId){
        OrderVO orderHistoryVo = ordersService.historyOrdersByOrderId(orderId);
        return Result.success(orderHistoryVo);
    }

    @PutMapping("/cancel/{id}")
    @Operation(summary = "取消订单")
    public Result<OrderVO> cancelOrder(@PathVariable("id")Long orderId){
        ordersService.userCancelByOrderId(orderId);
        return Result.success();
    }

    @PostMapping("/repetition/{id}")
    @Operation(summary = "再来一单")
    public Result repetition(@PathVariable("id") Long id){
        ordersService.repetition(id);
        return Result.success();
    }

    @GetMapping("/reminder/{id}")
    @Operation(summary = "催单")
    public Result reminder(@PathVariable("id") Long id){
        ordersService.reminder(id);
        return Result.success();
    }
}
